package com.example.testlxr9096;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.content.Intent;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;


import com.example.testlxr9096.itemActivity.SettingsActivity;
import com.example.testlxr9096.itemActivity.TestRecordActivity;
import com.example.testlxr9096.itemActivity.WangXianTestActivity;
import com.example.testlxr9096.itemActivity.WeiQianTestActivity;
import com.example.testlxr9096.itemActivity.XianLangTestActivity;

public class Home extends AppCompatActivity {

    private ImageView bt_xianlangTest,btn_weiqianTest,btn_wangxianTest,btn_settings,btn_dataExport;
    private TextView tv_connnectResult;
    private static final int REQUEST_CODE = 1;


    private String ip1 = "192.168.1.199";
    private String ip2 = "192.168.1.80";
    private int port1 = 502;
    private int port2 = 8080;
    private ModbusClientManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        tv_connnectResult = findViewById(R.id.tv_connectResult);
        initConnect();
        initView();


    }
    private void requestStoragePermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
        } else {
            // 已经拥有权限，可以进行存储操作

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CODE) {
            if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 用户授予了权限，可以进行存储操作

            } else {
                // 用户拒绝了权限，提示用户权限被拒绝
            }
        }
    }

   private void initConnect(){
       manager = ModbusClientManager.getInstance();
       manager.initializeDevices(ip1, port1, ip2, port2);
       updateStatus();
       new Thread(new Runnable() {
           @Override
           public void run() {
               manager.getDevice1().connect();
               manager.getDevice2().connect();
               runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
                       updateStatus();
                   }
               });


           }
       }).start();




   }

   public void initView(){

        bt_xianlangTest = findViewById(R.id.bt_xianLangTest);

        btn_wangxianTest = findViewById(R.id.bt_wangXianTest);

        btn_weiqianTest = findViewById(R.id.bt_weiQianTest);
        btn_settings = findViewById(R.id.bt_settings);
        btn_dataExport = findViewById(R.id.bt_dataExport);
        bt_xianlangTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Home.this, XianLangTestActivity.class);

                startActivity(intent);

            }
        });

        btn_wangxianTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Home.this, WangXianTestActivity.class);


                startActivity(intent);


            }
        });
        btn_weiqianTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Home.this, WeiQianTestActivity.class);


                startActivity(intent);
            }
        });
        btn_settings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Home.this, SettingsActivity.class);


                startActivity(intent);

            }
        });
        btn_dataExport.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Home.this, TestRecordActivity.class);


                startActivity(intent);

            }
        });
   }




    private void updateStatus() {
        String status = "设备1: " + (manager.getDevice1() != null && manager.getDevice1().isConnected() ? "已连接" : "未连接") +
                "\n设备2: " + (manager.getDevice2() != null && manager.getDevice2().isConnected() ? "已连接" : "未连接");
        tv_connnectResult.setText(status);
    }
}